package util;

/**
 * @author Administrator
 */
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.sql.Statement;  
  
public final class JdbcUtil { 
    private static String url = "jdbc:mysql://localhost:3306/";  
    private static String user = "root";  
    private static String password = "root";  
  
    //拒绝new一个实例  
    private JdbcUtil() {};  
  
    static {//注册驱动  
        try {  
            Class.forName("com.mysql.jdbc.Driver");  
        } catch (ClassNotFoundException e) {  
            throw new ExceptionInInitializerError(e);  
        }  
    }  
    //获取连接  
    public static Connection getConnection(String db) {  
    	Connection conn = null;
        try {
        	conn =  DriverManager.getConnection(url+db, user, password);
		} catch (SQLException e) {
			
			e.printStackTrace();
		} 
		return conn;
    }  
  
    //释放资源  
    public static void free(ResultSet rs, Statement stmt, Connection conn) {  
        if (rs != null) {  
            try {  
                rs.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            } finally {  
                if (stmt != null) {  
                    try {  
                        stmt.close();  
                    } catch (SQLException e) {  
                        e.printStackTrace();  
                    } finally {  
                        if (conn != null) {  
                            try {  
                                conn.close();  
                            } catch (SQLException e) {  
                                e.printStackTrace();  
                            }  
                        }  
                    }  
                }  
            }  
        }  
    }  
}  